package com.iris.java8.practice;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @author: xiaozhongsheng
 * @create: 2019-01-21 17:22
 **/
public class TraderMain {
    public static void main(String[] args) {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");
        List<Transaction> list = Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
        System.out.println("(1) 找出2011年发生的所有交易，并按交易额排序（从低到高）。");
        List<Transaction> result1 = list.stream().filter(transaction -> transaction.getYear()==2011)
                .sorted(Comparator.comparing(Transaction::getValue)).collect(Collectors.toList());
        System.out.println(result1);
        System.out.println("(2) 交易员都在哪些不同的ۡ市工作过？");
        System.out.println(list.stream().map(transaction -> transaction.getTrader().getCity()).distinct().collect(Collectors.toList()));
        System.out.println(list.stream().map(transaction -> transaction.getTrader().getCity()).collect(Collectors.toSet()));
        System.out.println("(3) 查找所有来自于剑桥的交易员，并按ހ名排序。");
        System.out.println(list.stream().filter(transaction -> "Cambridge".equals(transaction.getTrader().getCity())).collect(Collectors.toList()));
        System.out.println("(4) 返回所有交易员的ހ名字符串，按字母顺序排序。");
        System.out.println(list.stream().map(ll -> ll.getTrader().getName()).distinct().sorted(String::compareTo).reduce("",(a,b)-> a+" "+b));
        //高效方式
        System.out.println(list.stream().map(ll -> ll.getTrader().getName()).distinct().sorted(String::compareTo).collect(Collectors.joining(" ")));

        System.out.println("(5) 有没有交易员是在ዚ兰工作的？");
        System.out.println(list.stream().anyMatch(ll ->"Milan".equals(ll.getTrader().getCity())));
        System.out.println("(6) 打印生活在ғೃ的交易员的所有交易额。");
        System.out.println(list.stream().filter(ll -> "Milan".equals(ll.getTrader().getCity())).map(Transaction::getValue).collect(Collectors.toList()));
        System.out.println("(7) 所有交易中，最高的交易额是多少？");
        System.out.println(list.stream().map(Transaction::getValue).reduce(Integer::max));
        System.out.println(list.stream().map(Transaction::getValue).max(Integer::compare));
        System.out.println("(8) 找到交易额最小的交易");
        System.out.println(list.stream().map(Transaction::getValue).min(Integer::compare));
        System.out.println(list.stream().map(Transaction::getValue).min(Comparator.comparingInt(a -> a)));


        int month =12;
        double pre=0D;
        double total = 0D;
        int base = 5000;
        int totalGongzi = 0;
        int wuxianyijin = 691;
        int laoren = 0;
        int gongzi = 22000;
//        int wuxianyijin = 0;
//        int laoren = 1500;
//        int gongzi = 30000;
        double totalTax = 0D;
        for (int i = 1; i <= month; i++) {
            totalGongzi += gongzi;
            if(i==1){
                pre = (gongzi*i-base*i-wuxianyijin*i-laoren*i)*TraderMain.getPercent(totalGongzi).getPercent();
                total+=pre;
                System.out.println(pre);
            }else{
                pre = (gongzi*i-base*i-wuxianyijin*i-laoren*i)*TraderMain.getPercent(totalGongzi).getPercent()-TraderMain.getPercent(totalGongzi).getBase()-total;
                System.out.println("("+gongzi+"*"+i+"-"+base+"*"+i+"-"+wuxianyijin+"*"+i+"-"+laoren+"*"+i+")*"+TraderMain.getPercent(totalGongzi).getPercent()+"-"+TraderMain.getPercent(totalGongzi).getBase()+"-"+total);
                total+=pre;
                System.out.println(pre);
            }
            totalTax+=pre;
        }
        System.out.println("totalTax=="+totalTax);
    }

    public static Transaction getPercent(int money){

        if(money<=36000){
            return new Transaction(0.03, 0);
        }else if (money<=144000){
            return new Transaction(0.1, 2520);
        }else if (money<=300000){
            return new Transaction(0.2, 16920);
        }else if (money<=420000){
            return new Transaction(0.25, 31920);
        }else if (money<=660000){
            return new Transaction(0.3, 52920);
        }else if (money<=960000){
            return new Transaction(0.35, 86920);
        }else {
            return new Transaction(0.45, 181920);
        }
    }
}
